﻿using DBEntryHelper.Entity;
using Leafing.Data;
using Leafing.Data.Common;
using Leafing.Data.Definition;
using Leafing.Data.SqlEntry;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Xml;

namespace DBEntryHelper
{
    public partial class MainForm : Form
    {
        private DataTable dt= new DataTable();
        public MainForm()
        {
            InitializeComponent();
            IntiallizeDB();
        }
        private void IntiallizeDB()
        {
            XmlDocument xDoc = new XmlDocument();
            string path = Application.ExecutablePath;
            path = path.Remove(path.IndexOf("bin"));
            NameValueSectionHandler h = new NameValueSectionHandler();
            xDoc.Load(path + "App.config");
            XmlNode xNode = xDoc.SelectSingleNode("//Leafing.Settings");
            NameValueCollection l = (NameValueCollection)h.Create(null, null, xNode);
            this.cboEnv.Items.Clear() ;
            foreach (string key in l.Keys)
            {
                if (key.EndsWith(".DataBase"))
                {
                    this.cboEnv.Items.Add(key.Replace(".DataBase", ""));
                }
            }
        }


        private void RefreshDataSource()
        {
            try
            {
                DataProvider dv = new DataProvider(this.cboEnv.Text);
                //DbContext ds = new DbContext(this.cboEnv.Text);
                //List<string> sss = ds.GetTableNames();

                List<string> sss = dv.GetTableNames();
                dt = new DataTable();
                dt.Columns.Add("SEL", typeof(bool));
                dt.Columns.Add("TableName");
                checkedListBox1.Items.Clear();
                foreach (string s in sss)
                {
                    checkedListBox1.Items.Add(s);
                }
            }
            catch(Exception ex)
            {
                //throw new Exception(ex.Message.ToString());
                MessageBox.Show(ex.Message.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void cmdCreate_Click(object sender, EventArgs e)
        {
            ModelsGenerator mg = new();
            ModelsGenerator.SetEnv(this.cboEnv.Text, ckT.Checked);
        
            string sRe = "";
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                { 
                        string sTable  = checkedListBox1.GetItemText(checkedListBox1.Items[i]);
                       sRe = sRe + "\r\n" + ModelsGenerator.GenerateModelFromDatabase(sTable); 
                }
            }          
             this.txtResult.Text = sRe;
        }

        private void cboEnv_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshDataSource();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sRe = "";
            for(int i =0; i < 100; i ++)
            {
                string g = Guid.NewGuid().ToString();
                sRe = sRe + "\r\n" + g;

            }
            this.txtResult.Text = sRe;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            //测试指定库的访问，在新版本中通过在Model定义上面增加DbContext("MIDDB")的方式        //[DbTable("ERP_SALESORDER"),DbContext("MIDDB")]

            //DC_TASK obj = DBHelper.GetObject<DC_TASK>(CK.K["id"] == 160);

            List<XG_MAT> orders= (List<XG_MAT>)getObjects<XG_MAT>(null,"");

            string val = string.Empty;
        }

        public static IList<T> getObjects<T>(Condition? sWhereClause, string sorderby) where T : class, IDbObject, new()
        {
            if (sorderby.Length > 0)
            {
                return DbEntry.From<T>().Where(sWhereClause).OrderBy(new OrderBy(sorderby)).Select();
            }
            else
            {
                return DbEntry.From<T>().Where(sWhereClause).Select();
            }
        }
    }
}
